package jhunter.dao;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.sql.DataSource;

import jhunter.dao.dialect.Dialect;

public class DaoFactory {

	private Map<String, DataAccess> daos = new ConcurrentHashMap<String, DataAccess>();

	private EntityContainer entityContainer;

	private DataSource dataSource;
	
	private Dialect dialect;

	private Object object = new Object();

	public DataAccess getDao(String entityId) {
		if (!daos.containsKey(entityId)) {
			synchronized (object) {
				DefaultDataAccess da = new DefaultDataAccess(entityContainer, entityId);
				da.setDataSource(dataSource);
				da.setDialect(dialect);
				daos.put(entityId, da);
			}
		}
		return daos.get(entityId);
	}
	
	public void removeDao(String entityId) {
		daos.remove(entityId);
	}

	public EntityContainer getEntityContainer() {
		return entityContainer;
	}

	public void setEntityContainer(EntityContainer entityContainer) {
		this.entityContainer = entityContainer;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public Dialect getDialect() {
		return dialect;
	}

	public void setDialect(Dialect dialect) {
		this.dialect = dialect;
	}

}
